// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var transportNoticeId;
	var noticeState;
	var tools = require("tools");
    var left;
    var financeId;
    var constant = require("constant");
    var caseSizeMap = new tools.Map();
	var css_detail = require("./transportNoticeDetail.css");
	var html_transportNoticeFinish = require("./transportNoticeFinish.html");
	var init = function(page) {
		transportNoticeId = page.params.transportNoticeId;
		caseSizeMapToString();
		inquireTransportNoticeDetail();
		noticeState = page.params.noticeState||base.getUrlParam("noticeState");
    	left = base.getUrlParam("left");
		
		initEvent();
		router.init();
		
		
	};
	
	var caseSizeMapToString = function(){
		caseSizeMap.toString = function() {
			var str = "";
			for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
				if (this.container[keys[i]] == 0) {
					continue;
				}
				str = str  + this.container[keys[i]] + "X" + keys[i] + "<br/>";
			}
			return (str == "" ? "箱体情况：无" : "箱体情况：" + str);
		}
	}
	
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		//确认运输任务
		$("#confirmTrans").unbind("click").click(function() {
			confirmTransportNotice();
		});
		
		//取消运输任务
		$("#cancelTrans1").unbind("click").click(function() {
			dialog.showConfirmDialog("取消任务", "确认取消该运输任务？", function(){
				cancelTransportNotice();
			});
		});
		
		//保存运输信息
		$("#save").unbind("click").click(function() {
			if ($("#transportNoticeDetail .inventoryCheck:checked").length <= 0) {
				base.alert(2, "请选择货箱！");
				return;
			}
			
			$("#popdialog").html(html_transportNoticeFinish);
			$("#transportNoticeFinish_dialog .dialogContent").attr("action", "saveTransportInfo");
			$("#transportNoticeFinish_dialog .dialogTitle").html("保存到港信息	");
			$("#transportNoticeFinish_dialog #fileUuid").parent().remove();
			
			$.each($("#transportNoticeDetail .inventoryCheck:checked"), function() {
				var str = "<input type='hidden' name='inventoryId' value='" + $(this).attr("inventoryId") + "' />"
				$("#transportNoticeFinish_dialog .inventorys").append(str);
			});
			
			dialog.showDialog($("#transportNoticeFinish_dialog"));
			
			$("#transportNoticeFinish_dialog .sure_yes").unbind("click").click(function(){
				databind.submitDom($(".transportNoticeFinish"),function(){
					dialog.hideDialog($("#transportNoticeFinish_dialog"));
					base.alert(1, "保存成功！");
				});
			});
		});
		
		//完成运输任务
		$("#cancelTrans").unbind("click").click(function() {
			$("#popdialog").html(html_transportNoticeFinish);
			dialog.showDialog($("#transportNoticeFinish_dialog"));
			
			$("input[name=voyageNumber]").parent().parent().remove();
			$("input[name=harborDateTime]").parent().parent().remove();
			
			databind.fillForm($("#transportNoticeFinish_dialog"), {
				transportNoticeId: transportNoticeId
			});
			
			$("#transportNoticeFinish_dialog").on("change", "#fileUuid", function() {
				tools.uploadFile("fileUuid", function(data){
					var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>'
					$("#transportNoticeFinish_dialog label[for=fileUuid]").before(str);
					
					var fileUuid = [];
					for (var j = 0; j < $("#transportNoticeFinish_dialog .showProductIntroDiv .mas").length; j++) {
						fileUuid.push($($("#transportNoticeFinish_dialog .showProductIntroDiv .mas")[j]).attr("fileUuid"));
					}
					$("#transportNoticeFinish_dialog input[name=fileUuid]").val(fileUuid);
				})
			});
			
			$("#transportNoticeFinish_dialog").on("click", ".deleteProductIntro", function(){
				$(this).parent().remove();
				var fileUuid = [];
				for (var j = 0; j < $("#transportNoticeFinish_dialog .showProductIntroDiv .mas").length; j++) {
					fileUuid.push($($("#transportNoticeFinish_dialog .showProductIntroDiv .mas")[j]).attr("fileUuid"));
				}
				$("#transportNoticeFinish_dialog input[name=fileUuid]").val(fileUuid);
			});
				
			$("#sure_yes").unbind("click").click(function(){
				databind.submitDom($(".transportNoticeFinish"),function(){
					dialog.hideDialog($("#transportNoticeFinish_dialog"));
					location.href = "../transportNotice/transportNoticeList.html?noticeState=2&left=2";
				});
			});
		});
		
		//点击收货理货
		$(".transportNoticeList").unbind("click").click(function(){
			top.location.href = "../transportNotice/transportNoticeList.html?noticeState="+noticeState+"&left="+left;
		});
	};

	// 查询收货通知列表
	var inquireTransportNoticeDetail = function() {
		base.fnAjax({
			action: "inquireTransportNoticeDetail.json",
			oData: {
				recieveNoticeId: transportNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					financeId = data.datas[0].financeId;
					databind.fillInput($("#transportNoticeDetail"), {
						action: "inquireTransportNoticeDetail.json?recieveNoticeId=" + transportNoticeId
					});
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#noticeDetailTpl").html();
					var html = juicer(tpl, data);
					$("#noticeDetailBody").html(html);
					var noticeState = $("#noticeState").val();
					if (noticeState == 1) {
						$("#confirmTrans").removeClass("none");
						$("#cancelTrans1").removeClass("none");
					} else if (noticeState == 2) {
						$("#cancelTrans").removeClass("none");
						$("#save").removeClass("none");
					}
					inquireFinanceApplicationDetail();
					showCaseInfo(data);
					
					tools.addCheckAllEvent($("#transportNoticeDetail .inventoryCheck"), $("#transportNoticeDetail .inventoryCheckAll"));
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	var showCaseInfo = function(data){
		caseSizeMap.put("10GP", 0);
			caseSizeMap.put("20GP", 0);
			caseSizeMap.put("40GP", 0);
			caseSizeMap.put("48GP", 0);
			caseSizeMap.put("10HQ", 0);
			caseSizeMap.put("20HQ", 0);
			caseSizeMap.put("40HQ", 0);
			caseSizeMap.put("48HQ", 0);
			caseSizeMap.put("10RH", 0);
			caseSizeMap.put("20RH", 0);
			caseSizeMap.put("40RH", 0);
			caseSizeMap.put("48RH", 0);
			$.each(data.datas, function(i,val) {
				$.each(val.containerInventorys, function(i,item) {
					statisticalCaseSize(caseSizeMap, item.caseSize);
				});
			});
			$(".case_info").html(caseSizeMap.toString());
				
	}
	
	var statisticalCaseSize = function(m, c) {
		switch (c){
			case "10GP":
				m.put("10GP", m.get("10GP")*1 + 1);
				break;
			case "20GP":
				m.put("20GP", m.get("20GP")*1 + 1);
				break;
			case "40GP":
				m.put("40GP", m.get("40GP")*1 + 1);
				break;
			case "48GP":
				m.put("48GP", m.get("48GP")*1 + 1);
				break;
			case "10HQ":
				m.put("10HQ", m.get("10HQ")*1 + 1);
				break;
			case "20HQ":
				m.put("20HQ", m.get("20HQ")*1 + 1);
				break;
			case "40HQ":
				m.put("40HQ", m.get("40HQ")*1 + 1);
				break;
			case "48HQ":
				m.put("48HQ", m.get("48HQ")*1 + 1);
				break;
			case "10RH":
				m.put("10RH", m.get("10RH")*1 + 1);
				break;
			case "20RH":
				m.put("20RH", m.get("20RH")*1 + 1);
				break;
			case "40RH":
				m.put("40RH", m.get("40RH")*1 + 1);
				break;
			case "48RH":
				m.put("48RH", m.get("48RH")*1 + 1);
				break;
			default:
				break;
		}
	}

	// 完成运输通知
	var finishedTransportNotice = function() {
		base.fnAjax({
			action: "finishedTransportNotice.json",
			oData: {
				transportNoticeId: transportNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "任务已完成！");
					$("#confirmTrans").addClass("none");
					$("#cancelTrans").addClass("none");
					$("#save").addClass("none");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	// 确认运输通知
	var confirmTransportNotice = function() {
		base.fnAjax({
			action: "confirmTransportNotice.json",
			oData: {
				transportNoticeId: transportNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "任务确认成功");
					$("#confirmTrans").addClass("none");
					$("#cancelTrans").removeClass("none");
					$("#save").removeClass("none");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	// 确认运输通知
	var cancelTransportNotice = function() {
		base.fnAjax({
			action: "cancelTransportNotice.json",
			oData: {
				transportNoticeId: transportNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "任务取消成功");
					location.href = "../transportNotice/transportNoticeList.html";
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	// 融资详情
	var inquireFinanceApplicationDetail = function() {

		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			oData: {
				financeId: financeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#bookingSpaceTpl").html();
					var html = juicer(tpl, data);
					$("#bookingSpaceBody").append(html);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});